package com.frank.loadinglibrary.common;

import com.frank.loadinglibrary.utils.Indicator;
import com.frank.loadinglibrary.utils.OverWatchViewItem;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.app.Context;

import java.util.ArrayList;

public class OverWatchLoadComponent extends BaseLoadingComponent implements Component.DrawTask {

    public static final int OFFSET_SCALE = 20;
    private int mCenterX, mCenterY, mViewLength;
    private AnimatorGroup mPlayAnimator = new AnimatorGroup();
    private ArrayList<OverWatchViewItem> items = new ArrayList<>();
    protected Color color = Color.WHITE;
    private Indicator indicator;

    public OverWatchLoadComponent(Context context) {
        this(context, null);
    }

    public OverWatchLoadComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }

    public OverWatchLoadComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init();
    }

    public OverWatchLoadComponent(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
        init();
    }

    private void init() {
        addDrawTask(this);
    }

    private void initAnimation() {
        ArrayList<AnimatorValue> animators = new ArrayList<>();
        for (OverWatchViewItem item : items) {
            animators.add(item.getShowAnimation());
        }
        for (OverWatchViewItem item : items) {
            animators.add(item.getHideAnimation());
        }
        AnimatorGroup.Builder animatorGroupBuilder = mPlayAnimator.build();
        animators.forEach((AnimatorValue animation) -> {
            animatorGroupBuilder.addAnimators(animation);
        });
    }

    private void initViewItems() {
        if (items == null || items.isEmpty()) {
            Point[] points = getHexagonPoints(new Point(mCenterX, mCenterY), mViewLength);
            int itemLength = mViewLength / 2;
            int offsetLength = itemLength / OFFSET_SCALE;
            for (Point point : points) {
                items.add(new OverWatchViewItem(this, color, point, itemLength - offsetLength));
            }
            initAnimation();
            indicator = new Indicator<AnimatorGroup>() {

                @Override
                public ArrayList<AnimatorGroup> onCreateAnimators() {
                    ArrayList<AnimatorGroup> group = new ArrayList<>();
                    mPlayAnimator.setLoopedCount(-1);
                    group.add(mPlayAnimator);
                    return group;
                }
            };
            if (autoStart) {
                indicator.start();
            }
        }
    }


    /**
     * @param centerPoint
     * @param length
     * @return 7个点 中心点+6个顶点
     */
    public static Point[] getHexagonPoints(Point centerPoint, int length) {

        Point[] points = new Point[7];
        float height = (float) (Math.sin(Math.PI / 3) * length);
        points[0] = new Point(centerPoint.getPointX() - length / 2, centerPoint.getPointY() - height);
        points[1] = new Point(centerPoint.getPointX() + length / 2, centerPoint.getPointY() - height);
        points[2] = new Point(centerPoint.getPointX() + length, centerPoint.getPointY());
        points[3] = new Point(centerPoint.getPointX() + length / 2, centerPoint.getPointY() + height);
        points[4] = new Point(centerPoint.getPointX() - length / 2, centerPoint.getPointY() + height);
        points[5] = new Point(centerPoint.getPointX() - length, centerPoint.getPointY());
        points[6] = centerPoint;
        return points;

    }

    public void resetAllItem() {
        for (OverWatchViewItem item : items) {
            item.reset();
        }
        invalidate();
    }


    public void setColor(Color color) {
        this.color = color;
        if (items != null) {
            for (OverWatchViewItem item : items) {
                item.setColor(color);
            }
        }
    }

    @Override
    protected void stopAnimators() {
        if (indicator != null && indicator.isRunning()) {
            indicator.stop();
        }
    }

    @Override
    protected void startAnimators() {
        if (indicator != null && !indicator.isRunning()) {
            indicator.start();
        }
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        mCenterX = getWidth() / 2;
        mCenterY = getHeight() / 2;
        mViewLength = Math.min(getWidth(), getHeight()) / 3;
        initViewItems();
        for (OverWatchViewItem item : items) {
            item.drawViewItem(canvas);
        }
    }
}
